<%--
  Created by IntelliJ IDEA.
  User: chen
  Date: 17-8-18
  Time: 下午9:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>this is AddProduct.jsp</title>
    <script src="js/jquery-3.2.1.min.js"></script>
</head>
<body>
    <table cellspacing="0px">
        <tr>
            <td>
                <label for="name">Name:</label>
            </td>
            <td>
                <input id="name"/>
            </td>
        </tr>
        <tr>
            <td>
                <label for="price">Price</label>
            </td>
            <td>
                <input id="price"/>
            </td>
        </tr>
        <tr>
            <td align="center" valign="middle" colspan="2">
                <button id="_btn_addProduct" style="width: 100%;background: lightslategrey">添加</button>
            </td>
        </tr>

        <script type="text/javascript">
            var _btn = $("button#_btn_addProduct");
            var _name = $("input#name");
            var _price = $("input#price");
            function failure() {
                alert("Increase product failure");
                _name.val("");
                _price.val("");
            }
            $("#_btn_addProduct").click(function () {
                    if("" === _name.val() || "" === _price.val()){
                        alert("请输入完整")
                        return;
                    }
                    $.ajax({
                        url:"addProduct",
                        type:"POST",
                        data:{
                            name:_name.val(),
                            price:_price.val()
                        },
                        success:function (data) {
                            if(data ==="success"){
                                alert("Increase Product Success");
                                window.location.href = "product";
                            }else{
                                failure()
                            }
                        },
                    });
            });
        </script>
    </table>
</body>
</html>
